/***
This figure plots changes in employment by wage quartile, reweighting across
industries and areas.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Load data
*-------------------------------------------------------------------------------

* Load employment national x income quartile data
project, uses("${root}/data/web/data/Employment - National - Weekly.csv")
import delimited "${root}/data/web/data/Employment - National - Weekly.csv", clear

gen date = mdy(month, day_endofweek, year)

keep date emp_incq1 emp_incq4

tempfile q1_q4
save `q1_q4'

* Load employment data
project, uses("${root}/data/web/data/Employment - National - Weekly.csv")
import delimited "${root}/data/web/data/Employment - National - Weekly.csv", clear

gen date = mdy(month, day_endofweek, year)

* Keep relevant variables
keep date emp_subset_unweighted_q1 emp_subset_unweighted_q4 emp_subset_reweighted_q1

merge 1:1 date using `q1_q4', nogen

foreach emp in emp_subset_unweighted_q1 emp_subset_unweighted_q4 emp_subset_reweighted_q1 emp_incq1 emp_incq4  {
	replace `emp' = `emp' * 100
}

* Restrict dates
keep if date <= `=${finaldate}'

* Set last date
sum date
di %td `r(max)'
local last_date = `r(max)'

* Make x-labels
local first_date = mdy(2, 14, 2020)

* Choose last point of graph
local end_graph = `last_date' + 60

* Make annotations
sum emp_subset_unweighted_q4 if date == `last_date'
assert `r(N)' == 1
local end_q4 : di %3.1f `r(mean)'
local end_q4_str = cond(`end_q4' > 0, "+`end_q4'%", "`end_q4'%")
sum emp_subset_unweighted_q1 if date == `last_date'
assert `r(N)' == 1
local end_q1 : di %3.1f `r(mean)'
local end_q1_str = cond(`end_q1' > 0, "+`end_q1'%", "`end_q1'%")
sum emp_subset_reweighted_q1 if date == `last_date'
assert `r(N)' == 1
local emp_subset_reweighted_q1 : di %4.1f `r(mean)'
local end_q1_rwt_str = cond(`emp_subset_reweighted_q1' > 0, "+`emp_subset_reweighted_q1'%", "`emp_subset_reweighted_q1'%")

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

tw ///
	(line emp_subset_unweighted_q4 date if date <= ${finaldate}, color(oi2)) ///
	(line emp_subset_unweighted_q1 date if date <= ${finaldate}, color(oi1)) ///
	(line emp_subset_reweighted_q1 date if date <= ${finaldate}, color(oi1) lpattern(dash)) ///
	(scatter emp_subset_unweighted_q4 date if date == ${finaldate}, color(oi2)) ///
	(scatter emp_subset_unweighted_q1 date if date == ${finaldate}, color(oi1)) ///
	(scatter emp_subset_reweighted_q1 date if date == ${finaldate}, color(oi1)) ///
	, ///
	legend(off) ///
	text(2 `=mdy(11, 1, 2020)' "Top Wage Quartile", size(*0.92) color(oi2)) ///
	text(-12 `=mdy(11, 1, 2020)' "Bottom Wage Quartile, Reweighted", size(*0.92) color(oi1)) ///
	text(-14.2 `=mdy(11, 1, 2020)' "to Match Top Quartile on County x Industry", size(*0.75) color(oi1)) ///
	text(-24.5 `=mdy(11, 1, 2020)' "Bottom Wage Quartile", size(*0.92) color(oi1)) ///
	xline(${finaldate}, lcolor(gs12)) ///
	text(5.3 `=$finaldate + 50' "December 31 2021", place(9) color(gs7) size(2.5)) ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""'  `=mdy(3, 1, 2020)' "Mar" ///
	`=mdy(5, 1, 2020)' "May" ///
	`=mdy(7, 1, 2020)' "Jul"  `=mdy(9, 1, 2020)' "Sep" ///
	 `=mdy(11, 1, 2020)' "Nov"  ///
	`=mdy(1, 1, 2021)' `""Jan" "2021""'  `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul" `=mdy(9, 1, 2021)' "Sep" `=mdy(11, 1, 2021)' "Nov"  ///
	, format(%tdm) labsize(small)) ///
	ytitle("Change in Employment (%)" "Relative to January 2020") ///
	yline(0, lpattern(dash) lcolor(gs8)) ///
	ylab( -40 "-40%" -30 "-30%" -20 "-20%" -10 "-10%" 0 "0%", nogrid) ///
	ysc(r(0 -40)) ///
	text(`=`end_q4'' `=`last_date' + 42' "`end_q4_str'", color(oi2) size(medsmall)) ///
	text(`=`end_q1'-0.3' `=`last_date' + 42' "`end_q1_str'", color(oi1) size(medsmall)) ///
	text(`=`emp_subset_reweighted_q1' + 0.5' `=`last_date' + 42' "`end_q1_rwt_str'", color(oi1) size(medsmall)) ///
	xtitle("") ///
	b1title("       ", color(white) pos(3) ring(5)) ///
	${title_`version'}
oi_graph_export "${root}/results/Employment/Changes in Employment by Income Quartile Reweighting - long", type(${fig_type})

* Black and white version for QJE
tw ///
	(line emp_subset_unweighted_q4 date if date <= ${finaldate}, color(gs8)) ///
	(line emp_subset_unweighted_q1 date if date <= ${finaldate}, color(gs0)) ///
	(line emp_subset_reweighted_q1 date if date <= ${finaldate}, color(gs0) lpattern(dash)) ///
	(scatter emp_subset_unweighted_q4 date if date == ${finaldate}, color(gs8)) ///
	(scatter emp_subset_unweighted_q1 date if date == ${finaldate}, color(gs0)) ///
	(scatter emp_subset_reweighted_q1 date if date == ${finaldate}, color(gs0)) ///
	, ///
	legend(off) ///
	text(2 `=mdy(11, 1, 2020)' "Top Wage Quartile", size(*0.92) color(gs8)) ///
	text(-12 `=mdy(11, 1, 2020)' "Bottom Wage Quartile, Reweighted", size(*0.92) color(gs0)) ///
	text(-14.2 `=mdy(11, 1, 2020)' "to Match Top Quartile on County x Industry", size(*0.75) color(gs0)) ///
	text(-24.5 `=mdy(11, 1, 2020)' "Bottom Wage Quartile", size(*0.92) color(gs0)) ///
	xline(${finaldate}, lcolor(gs12)) ///
	text(5.3 `=$finaldate + 50' "December 31 2021", place(9) color(gs7) size(2.5)) ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""'  `=mdy(3, 1, 2020)' "Mar" ///
	`=mdy(5, 1, 2020)' "May" ///
	`=mdy(7, 1, 2020)' "Jul"  `=mdy(9, 1, 2020)' "Sep" ///
	 `=mdy(11, 1, 2020)' "Nov"  ///
	`=mdy(1, 1, 2021)' `""Jan" "2021""'  `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul" `=mdy(9, 1, 2021)' "Sep" `=mdy(11, 1, 2021)' "Nov"  ///
	, format(%tdm) labsize(small)) ///
	ytitle("Change in Employment (%)" "Relative to January 2020") ///
	yline(0, lpattern(dash) lcolor(gs8)) ///
	ylab( -40 "-40%" -30 "-30%" -20 "-20%" -10 "-10%" 0 "0%", nogrid) ///
	ysc(r(0 -40)) ///
	text(`=`end_q4'' `=`last_date' + 42' "`end_q4_str'", color(gs8) size(medsmall)) ///
	text(`=`end_q1'-0.3' `=`last_date' + 42' "`end_q1_str'", color(gs0) size(medsmall)) ///
	text(`=`emp_subset_reweighted_q1' + 0.5' `=`last_date' + 42' "`end_q1_rwt_str'", color(gs0) size(medsmall)) ///
	xtitle("") ///
	b1title("       ", color(white) pos(3) ring(5)) ///
	${title_`version'}

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_5b.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_5b.svg")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile, Reweighting Across Industries and Areas.yaml"

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile, Reweighting Across Industries and Areas.yaml", ///
	key("emp_dec_q1_uw") ///
	comment("Change in Employment (%) Relative to January 2020 - Income Q1 - April 15 2022") ///
	value(`end_q1') fmt(%9.2f)

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile, Reweighting Across Industries and Areas.yaml", ///
	key("emp_dec_q1_rw") ///
	comment("Change in Employment (%) Relative to January 2020 - Income Q1 Reweighted to Match Top Q on County x Industry - April 15 2022") ///
	value(`emp_subset_reweighted_q1') fmt(%9.2f)

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile, Reweighting Across Industries and Areas.yaml", ///
	key("emp_dec_q4_rw") ///
	comment("Change in Employment (%) Relative to January 2020 - Income Q4 - April 15 2022") ///
	value(`end_q4') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile, Reweighting Across Industries and Areas.yaml")
